package com.tsl.controller;


import com.burial.common.log.MyLog;
import com.burial.common.result.Result;
import com.tsl.domain.dto.PageDto;
import com.tsl.domain.dto.TbSKUDto;
import com.tsl.domain.pojo.TbSKU;
import com.tsl.domain.vo.PageVo;
import com.tsl.service.ITbProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author 唐书亮
 * @since 2024-04-17
 */
@RestController
@Api(tags = "商品管理")
@RequestMapping("/product")
public class TbProductController {
    @Autowired
    private ITbProductService tbProductService;

    @ApiOperation("添加商品接口")
    //添加商品
    @PostMapping("/addproduct")
    @MyLog(type = "商品信息更新", msg = "新增一条商品信息")
    public Result test(@RequestBody TbSKUDto tbSKUDto) {
        //1.添加商品到数据库
        Boolean isSave = tbProductService.saveProduct(tbSKUDto);
        if (isSave) {
            return Result.success(tbSKUDto);
        }
        return Result.error("保存失败");
    }

    //分页查询所有商品
    @GetMapping("/listPage")
    @ApiOperation("分页查询所有商品接口")
    @MyLog(type = "商品信息检索", msg = "分页查询所有商品信息")
    public Result listProduct(Integer page, Integer pageSize,String productName) {
        //封装pageDto
        PageDto pageDto = new PageDto(page, pageSize,productName);
        // 调用service层方法
        PageVo pageVo = tbProductService.listPageProduct(pageDto);
        if (ObjectUtils.isEmpty(pageVo)) {
            return Result.error("数据查询失败");
        }
        //返回数据
        return Result.success(pageVo);
    }

    //根据id查询商品
    @GetMapping("/selectBy/{id}")
    @ApiOperation("根据id查询商品信息接口")
    @MyLog(type = "商品信息检索", msg = "根据id检索商品信息")
    public Result selectById(@PathVariable(value = "id") Integer id) {
        TbSKU tbSKU = tbProductService.selectById(id);
        if (ObjectUtils.isEmpty(tbSKU)) {
            return Result.error("没有这个商品");
        }
        return Result.success(tbSKU);
    }

    //更新商品信息
    @PutMapping("/updateProduct")
    @ApiOperation("更新商品信息接口")
    @MyLog(type = "商品信息更新", msg = "更新一条商品信息")
    public Result updateProduct(@RequestBody TbSKU tbSKU) {
        Boolean isUpdate = tbProductService.updateProduct(tbSKU);
        if (isUpdate) {
            return Result.success();
        }
        return Result.error("更新失败");
    }

    //删除商品信息
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("删除商品接口")
    @MyLog(type = "商品信息更新", msg = "删除一条商品信息")
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        Boolean isDelete = tbProductService.deleteProduct(id);
        if (isDelete) {
            return Result.success();
        }
        return Result.error("删除失败");
    }

    //下架商品
    @GetMapping("/updateProductState")
    @ApiOperation("修改商品状态接口")
    @MyLog(type = "修改商品状态", msg = "修改一个商品的状态")
    public Result stopProductById( Integer id,Integer step) {

     Boolean isUpdate = tbProductService.updateProductStateById(id,step);

        if (isUpdate) {
            return Result.success();
        }
        return Result.error("修改失败");

    }
}
